﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using Distribution.Data;
using Distribution.General;
using Entities;

namespace Distribution.Service
{
    public class LMeter
    {
        #region - Public methods -

        public static DataTable GetMeterDetails(EPole pole)
        {
            return DMeter.GetMeterDetails(pole);
        }

        public static EMeter GetById(int id)
        {
            return DMeter.GetById(id);
        }

        public static void Save(EMeter meter)
        {
            DMeter.Save(meter);

            if (meter.Image != null)
            {
                FileInfo _file = new FileInfo(PPIConstants.K_ImagesFolder + meter.Image.TempFileName);

                if (_file.Exists && !meter.Image.IsDeleted && meter.Image.Id == Int32.MinValue)
                    meter.Image.FileName = _file.CopyTo(PPIConstants.K_ImagesFolder + meter.Image.FileName, true).Name;
            }
        }

        public static void Update(EMeter meter)
        {
            DMeter.Update(meter);

            if (meter.Image != null)
            {
                if (!meter.Image.FileName.Equals(meter.Image.TempFileName))
                {
                    FileInfo _file = new FileInfo(PPIConstants.K_ImagesFolder + meter.Image.TempFileName);

                    if (_file.Exists && !meter.Image.IsDeleted && meter.Image.Id == Int32.MinValue)
                        meter.Image.FileName = _file.CopyTo(PPIConstants.K_ImagesFolder + meter.Image.FileName, true).Name;
                }
            }
        }

        public static void Delete(EPole pole)
        {
            List<EMeter> meters = DMeter.GetAll(pole);

            DMeter.Delete(meters);
        }

        public static void Delete(EMeter meter)
        {
            DMeter.Delete(meter);
        }

        public static void Delete(List<EMeter> meters)
        {
            DMeter.Delete(meters);
        }

        public static List<EMeter> GetAll(EPole pole)
        {
            return DMeter.GetAll(pole);
        }

        #endregion
    }
}
